﻿using MySql.Data.MySqlClient;
using PelitaProduksi.Controller;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PelitaProduksi.Model
{
    public class Barang : BaseProduksi
    {
        public Barang() {}

        public static List<Barang> listAllBarang(bool filter)
        {
            string add = "";
            if (filter) add = "where aktif = 1 ";
            List<Barang> list = new List<Barang>();
            MySQLController db = new MySQLController();
            db.Open();
            MySqlDataReader reader = db.ExecuteReader(string.Format(
                "select * from barang {0} order by nama asc", add));
            while (reader.Read())
            {
                Barang b = new Barang();
                b.Kode = reader.GetString(0);
                b.Nama = reader.GetString(1);
                b.Tupdate = reader.GetString(2);
                b.Jupdate = reader.GetString(3);
                b.Usrupdate = reader.GetString(4);
                b.Keterangan = reader.GetString(5);
                b.Aktif = reader.GetBoolean(6);
                list.Add(b);
            }
            db.Close();
            return list;
        }

        public override string ToString()
        {
            return Nama;
        }
        string kode;

        public string Kode
        {
            get { return kode; }
            set { kode = value; }
        }
        string nama;

        public string Nama
        {
            get { return nama; }
            set { nama = value; }
        }
        string tupdate;

        public string Tupdate
        {
            get { return tupdate; }
            set { tupdate = value; }
        }
        string jupdate;

        public string Jupdate
        {
            get { return jupdate; }
            set { jupdate = value; }
        }
        string usrupdate;

        public string Usrupdate
        {
            get { return usrupdate; }
            set { usrupdate = value; }
        }
        string keterangan;

        public string Keterangan
        {
            get { return keterangan; }
            set { keterangan = value; }
        }
        bool aktif;

        public bool Aktif
        {
            get { return aktif; }
            set { aktif = value; }
        }

        public override bool Equals(object obj)
        {
            try
            {
                Barang ini = obj as Barang;
                bool ok = true;
                List<bool> test = new List<bool>();
                test.Add(ini.Kode.Equals(this.Kode));
                test.Add(ini.Nama.Equals(this.Nama));
                test.Add(ini.Aktif.Equals(this.Aktif));
                test.Add(ini.Keterangan.Equals(this.Keterangan));
                foreach (bool b in test)
                {
                    ok = ok & b;
                }
                return ok;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override void insert()
        {
            MySQLController db = new MySQLController();
            db.Open();
            MySqlTransaction tr = db.BeginTransaction();
            string queryInsert = string.Format(
                "insert into {0} (kode, nama, aktif, keterangan, tupdate, jupdate, usrupdate) " +
            "VALUES('{1}', '{2}', {3}, '{4}', '{5}', '{6}', '{7}'')",
                "barang",
                this.Kode,
                this.Nama,
                this.Aktif,
                this.Keterangan,
                this.Tupdate,
                this.Jupdate,
                this.Usrupdate
                );
            try
            {
                db.ExecuteNonQuery(queryInsert);
                tr.Commit();
            }
            catch (Exception)
            {
                tr.Rollback();
            }
            db.Close();
        }

        public override void edit(string id)
        {
            MySQLController db = new MySQLController();
            db.Open();
            MySqlTransaction tr = db.BeginTransaction();
            string queryUpdate = string.Format(
                "update {0} SET nama='{1}', keterangan='{2}', aktif={3}, tupdate='{5}', jupdate='{6}', usrupdate='{7}'where kode='{4}'",
                "barang",
                this.Nama,
                this.Keterangan,
                this.Aktif,
                this.Kode,
                this.Tupdate,
                this.Jupdate,
                this.Usrupdate
                );
            try
            {
                db.ExecuteNonQuery(queryUpdate);
                tr.Commit();
            }
            catch (Exception)
            {
                tr.Rollback();
            }
            db.Close();
        }
    }
}
